
var _util = require('util/util.js');
var _order_service = require('service/order-service.js');
var listTemplate = require('./index.string');

require('page/common/header/index.js');

var newOrder = {
    listDate : {
        itemList    : {}
    },
    init: function () {
        this.bindEvents();
        this.loadAccountInfo();
        return this;
    },
    bindEvents: function () {
        $('#Continue').click (
            function () {
                window.location.href = './catalog-main.html';
            }
        )
    },
    continueclict: function (usernameasd) {
        console.log("Continue2");
        var account = {
            username                : usernameasd,
            expiryDate              : $('#expiryDate').val(),
            billToFirstName         : $('#billToFirstName').val(),
            billToLastName          : $('#billToLastName').val(),
            creditCard              : $('#creditCard').val(),
            billAddress1            : $('#billAddress1').val(),
            billAddress2            : $('#billAddress2').val(),
            billCity                : $('#billCity').val(),
            billState               : $('#billState').val(),
            billZip                 : $('#billZip').val(),
            billCountry             : $('#billCountry').val(),
            cardType                : $('#cardType').val(),
            shippingAddressRequired : $('#oCheckbox').prop('checked'),
            shipToFirstName         : $('#shipToFirstName').val(),
            shipToLastName          : $('#shipToLastName').val(),
            shipAddress1            : $('#shipAddress1').val(),
            shipAddress2            : $('#shipAddress2').val(),
            shipCity                : $('#shipCity').val(),
            shipState               : $('#shipState').val(),
            shipZip                 : $('#shipZip').val(),
            shipCountry             : $('#shipCountry').val()
        };
        _order_service.confirmOrder(
            account,
            function(res) {
                $('#shipping').hide();
                $('#Billing').hide();
                $('#oCheckbox').hide;
                console.log(res);
                window.location.href = './order-confirmOrder.html';
            },
            function(errMsg) {
            }
        )
    },
    loadAccountInfo : function(){
        var id = _util.getURLParam('id');
        var route = _util.getURLParam('route');
        var _this = this;
        if(id === 'newOrder'){
            _order_service.createOrder(  
                "j2ee",
                function(res){
                    console.log(res);
                    _this.listDate.itemList = res;

                    var result = _util.renderHtml(listTemplate, {ListData : _this.listDate});
                    $('#newOrderForm').html(result);
                    $('#shipping').hide();
                    document.getElementById("Continue").addEventListener("click",function(){
                        _this.continueclict(route);
                    });
                },
                function(errMsg) {
                    console.log('errMsg...'+errMsg);
                }
            );
        }
    }
}
module.exports = newOrder.init();